package api

import (
	"fmt"
	"gateway/basic/config"
	__ "gateway/basic/proto/userproto"
	"gateway/handler/request"
	"gateway/handler/response"
	"gateway/pkg"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"github.com/share309/gojwt"
	"time"
)

// RegisterFaceInfo
// @Summary      注册人脸信息
// @Description  上传并注册用户人脸信息
// @Tags         人脸模块
// @Accept       multipart/form-data
// @Produce      json
// @Param        file  formData  file  true  "人脸图片文件"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /face/register [post]
func RegisterFaceInfo(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		response.ParameterError(c, "文件上传失败", err.Error())
		return
	}
	open, _ := file.Open()
	url, err := pkg.QiNiuYun(open, file.Filename)
	defer open.Close()
	if err != nil {
		response.ParameterError(c, "获取图像失败", err.Error())
		return
	}

	userId := c.GetUint("userId")
	fmt.Println(userId)
	res, err := config.UserClient.RegisterFace(c, &__.RegisterFaceReq{
		UserId: int32(c.GetUint("userId")),
		Img:    url,
	})
	if err != nil {
		response.SrvError(c, "注册人脸信息失败", err.Error())
		return
	}
	response.Success(c, fmt.Sprintf("注册人脸信息成功,id:%v", res.Id), url)
}

// MatchFaceInfo
// @Summary      人脸匹配
// @Description  匹配人脸信息并生成token
// @Tags         人脸模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.FaceMatch  true  "人脸匹配参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /face/match [post]
func MatchFaceInfo(c *gin.Context) {
	var req request.FaceMatch
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.UserClient.MatchFaceInfo(c, &__.MatchFaceInfoReq{
		Img:    req.Img,
		UserId: req.UserId,
	})
	if err != nil {
		response.SrvError(c, "对比人脸信息失败", err.Error())
		return
	}
	token, err := gojwt.NewJWT(config.JWTKEY).CreateToken(gojwt.CustomClaims{
		ID: uint(res.UserId),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 3600*3,
		},
	})
	if err != nil {
		response.ParameterError(c, "token生成失败", err.Error())
		return
	}
	response.Success(c, "对比人脸信息成功", token)
}

// UpdateFaceInfo
// @Summary      更新人脸信息
// @Description  更新用户人脸信息
// @Tags         人脸模块
// @Accept       multipart/form-data
// @Produce      json
// @Param        file  formData  file  true  "新的人脸图片文件"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /face/update [post]
func UpdateFaceInfo(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		response.ParameterError(c, "文件上传失败", err.Error())
		return
	}
	open, _ := file.Open()
	url, err := pkg.QiNiuYun(open, file.Filename)
	defer open.Close()
	if err != nil {
		response.ParameterError(c, "获取图像失败", err.Error())
		return
	}

	userId := c.GetUint("userId")
	_, err = config.UserClient.UpdateFaceInfo(c, &__.UpdateFaceInfoReq{
		UserId: int32(userId),
		Img:    url,
	})
	if err != nil {
		response.ParameterError(c, "修改人脸图像失败", err.Error())
		return
	}
	response.Success(c, "修改人脸图像成功", url)
}

// DeleteFaceInfo
// @Summary      删除人脸信息
// @Description  删除用户人脸信息
// @Tags         人脸模块
// @Accept       json
// @Produce      json
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /face/delete [post]
func DeleteFaceInfo(c *gin.Context) {
	userId := c.GetUint("userId")
	res, err := config.UserClient.DeleteFaceInfo(c, &__.DeleteFaceInfoReq{
		UserId: int32(userId),
	})
	if err != nil {
		response.SrvError(c, "删除人脸信息失败", err.Error())
		return
	}
	response.Success(c, "删除人脸信息成功", res)
}
